package com.voxel.sense.infomsg.module.user.staff;

import android.text.TextUtils;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.utils.manager.QueryConfigManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.SearchMenuEntity;
import cyy.example.com.facotry.model.api.response.StaffEntity;
import cyy.example.com.facotry.net.helper.NetHelper;

/**
 * Created by ChenYiYao on 2018/7/26.
 */
public class StaffPresenter extends RecyclerViewPresenter<StaffContract.View>
        implements StaffContract.Presenter, DataSource.Callback<BaseEntity<StaffEntity>> {
    private Map<String, Object> mQueryMap;//选择添加的条件

    //加载类型
    private String mUserStatusType;


    public StaffPresenter(StaffContract.View view,String userStatus) {
        super(view);
        mUserStatusType = userStatus;
    }



    @Override
    public void start() {
        super.start();
        EventBus.getDefault().register(this);
        mQueryMap = new HashMap<>();
        mQueryMap.put(Constants.TYPE_DEPARTMENT_TYPE, new ArrayList());
        mQueryMap.put(Constants.TYPE_USER_ROLE, new ArrayList());
    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void loadData() {
        netLoadData(getPAGE_NUMB());
    }

    @Override
    protected void loadMoreData() {
        netLoadData(getPAGE_NUMB());
    }

    private void netLoadData(int PAGE_Numb) {
        List departType = (List) mQueryMap.get(Constants.TYPE_DEPARTMENT_TYPE);
        List<String> roleType = (List<String>) mQueryMap.get(Constants.TYPE_USER_ROLE);
        List<Integer> id = new ArrayList<>();
        for(int i = 0; i < roleType.size(); i++){
            id.add(Integer.parseInt(roleType.get(i)));
        }
        NetHelper.getStaffList(PAGE_Numb, getPageDataSize(), departType, id,mUserStatusType, this);

    }

    @Override
    public void doSearchCondition() {
        SearchMenuEntity userRole = QueryConfigManager.getInstance().getRoleType();
        SearchMenuEntity deparment = QueryConfigManager.getInstance().getDepartmentType();
        List<ConditionBean> conditionBeans = CoverMenuData(deparment, Constants.TYPE_DEPARTMENT_TYPE);
        List<ConditionBean> conditionBeans1 = CoverMenuData(userRole, Constants.TYPE_USER_ROLE);
        mView.onRefreshMenu(conditionBeans, conditionBeans1);
    }

    private List<ConditionBean> CoverMenuData(SearchMenuEntity entity, String type) {
        List<ConditionBean> datas = new ArrayList<>();
        if (entity == null || entity.getStaticSystemConstantDetails() == null ||
                entity.getStaticSystemConstantDetails().size() <= 0) {
            return datas;
        }
        List<SearchMenuEntity.StaticSystemConstantDetailsBean> conditions = entity.getStaticSystemConstantDetails();
        for (SearchMenuEntity.StaticSystemConstantDetailsBean bean : conditions) {
            String icon = "";
            ConditionBean conditionBean = new ConditionBean(type, bean.getConstantCode(),
                    bean.getConstantName(), icon, false);
            datas.add(conditionBean);
        }
        return datas;
    }

    @Override
    public void doLoadData() {
        startRefreshData();
    }

    @Override
    public void doLoadMoreData() {
        startLoadMoreData();
    }

    @Override
    public void putSearTarget(String type, String value) {
        if (Constants.TYPE_DEPARTMENT_TYPE.equals(type)) {
            List list = (List) mQueryMap.get(type);
            list.add(value);

        } else if (Constants.TYPE_USER_ROLE.equals(type)) {
            List list = (List) mQueryMap.get(type);
            list.add(value);
        }

    }

    @Override
    public void removeSearchData(String type, String value) {
        if (Constants.TYPE_DEPARTMENT_TYPE.equals(type)) {
            List list = (List) mQueryMap.get(type);
            list.remove(value);

        } else if (Constants.TYPE_USER_ROLE.equals(type)) {
            List list = (List) mQueryMap.get(type);
            list.remove(value);
        }
    }

    @Override
    public void doResetCondition(String type) {
        List list = (List) mQueryMap.get(type);
        list.clear();
    }


    @Override
    public void onDataLoaded(BaseEntity<StaffEntity> data, int reqId) {
        if (data.isSuccess() && mView != null) {
//            List<StaffEntity> bean = data.getData();
            StaffEntity bean = data.getData();
            List<StaffEntity.SysUserListBean> sysUserList = bean.getSysUserList();
            if (sysUserList != null) {
                loadDataComplete(sysUserList);
                return;
            }
        }
        LoadDataFailure();
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null)
            return;
        LoadDataFailure();
    }


    @Subscribe
    public void onEventBusThread(EventEntity entity) {
        if (Constants.INFOMARL_USER_YPE.equals(mUserStatusType) && EventBusConstants.CHECK_USER_SUCCESS.equals(entity.key)) {
            //审核类型列表，且是审核通知
            RecyclerAdapter<StaffEntity.SysUserListBean> mStaffAdapter = mView.getRecyclerViewAdapter();
            String value = (String) entity.value;
            if (!TextUtils.isEmpty(value) && mStaffAdapter != null && !ListUtil.isEmpty(mStaffAdapter.getItems())) {
                List<StaffEntity.SysUserListBean> items = mStaffAdapter.getItems();
                for (int i = items.size() - 1; i >= 0; i--) {
                    if (value.equals(items.get(i).getUserId())) {
                        items.remove(i);
                        mStaffAdapter.notifyDataSetChanged();
                        return;
                    }
                }
            }
        }
    }


}
